package com.hfy.user.controller;

import com.hfy.model.dto.post.CreateFavoriteDto;
import com.hfy.model.dto.post.DoCollectDto;
import com.hfy.model.dto.post.PostPostDto;
import com.hfy.model.po.user.post.PostDraftPo;
import com.hfy.model.po.user.post.PostLikePo;
import com.hfy.user.service.PostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import result.Result;

import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 韩
 * @since 2024-11-28
 */
@RestController
@RequestMapping("/post")
@Tag(name = "论坛帖子的控制器" ,description = "对帖子进行一些操作")
public class PostController {

    @Autowired
    private PostService postService;

    @PostMapping("/post")
    @Operation(summary = "发帖！", description = "发帖需要注意的是匿名和实名的不同处理")
    @Parameters({@Parameter(name = "content", description = "帖子内容是html格式"),
            @Parameter(name = "picList", description = "图片列表最多9张"),
            @Parameter(name = "topicId", description = "引用的话题"),
            @Parameter(name = "remark", description = "帖子的一个备注，仅自己可见"),
            @Parameter(name = "wordNum", description = "帖子的字数"),
            @Parameter(name = "account", description = "账号"),
            @Parameter(name = "isNameless", description = "是否匿名")})
    public Result postPost(@RequestBody Optional<PostPostDto> dto) {
        return postService.postPost(dto);
    }

    @GetMapping("/topics")
    @Operation(summary = "获取发帖系统的话题数据", description = "需要返回的是topic的id和value")
    @Parameters({})
    public Result getTopics() {
        return postService.getTopics();
    }

    @GetMapping("/drafts/list/{postId}")
    @Operation(summary = "获取帖子的草稿列表", description = "根据帖子ID获取相关的草稿列表，按时间序")
    @Parameters({
        @Parameter(name = "postId", description = "帖子ID", required = true)
    })
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功获取草稿列表",
            content = @Content(schema = @Schema(implementation = PostDraftPo.class)))
    })
    public Result listDraftsByTimes(@PathVariable Integer postId) {
        return postService.listDraftsByTimes(postId);
    }

    @PostMapping("/draft/save")
    @Operation(summary = "保存草稿", description = "保存或更新草稿内容")
    @Parameters({
        @Parameter(name = "content", description = "草稿内容"),
        @Parameter(name = "postId", description = "关联的帖子ID"),
        @Parameter(name = "account", description = "用户账号"),
        @Parameter(name = "updateTime", description = "草稿更新的时间")
    })
    public Result saveDraft(@RequestBody PostDraftPo draft) {
        return postService.saveDraft(draft);
    }

    @GetMapping("/list/page")
    @Operation(summary = "分页获取帖子信息", description = "这个接口是首页的分页获取帖子信息，后期会有多个分页接口")
    @Parameters({
            @Parameter(name = "lastPostId", description = "上一次查询的最后一个postId"),
            @Parameter(name = "pageSize", description = "一次查询的页数")
    })
    public Result listAnyPostsByPage(@RequestParam("lastPostId") Optional<Integer> lastPostId, @RequestParam("pageNum") Optional<Integer> pageSize) {
        return postService.listAnyPostsByPage(lastPostId, pageSize);
    }

    @PostMapping("/like/do")
    @Operation(summary = "做帖子点赞和取消点赞操作", description = "通过account查询是否点赞然后执行不同操作")
    @Parameters({
            @Parameter(name = "postId", description = "帖子Id"),
            @Parameter(name = "account", description = "账号")
    })
    public Result toggleLikePost(@RequestBody PostLikePo dto) {
        return postService.toggleLikePost(Optional.of(dto.getPostId()), Optional.of(dto.getAccount()));
    }

    @GetMapping("/info/show")
    @Operation(summary = "查询帖子详情", description = "帖子详情页面发起获取信息的请求")
    @Parameters({
            @Parameter(name = "postId", description = "帖子id"),
            @Parameter(name = "account", description = "账号"),
    })
    private Result getThePostInfo(@RequestParam("postId") Optional<Integer> postIdOptional, @RequestParam("account") Optional<Long> accountOptional) {
        return postService.getPostInfo(postIdOptional, accountOptional);
    }

    @PostMapping("/favorite/collect")
    @Operation(summary = "做帖子收藏和取消收藏操作", description = "通过account查询是否点赞然后执行不同操作")
    @Parameters({
            @Parameter(name = "postId", description = "帖子Id"),
            @Parameter(name = "account", description = "账号"),
            @Parameter(name = "favoriteId", description = "收藏夹Id")
    })
    public Result toggleCollectPost(@RequestBody Optional<DoCollectDto> dtoOptional) {
        return postService.toggleCollectPost(dtoOptional);
    }

    @GetMapping("/favorites/list/{account}-{postId}")
    @Operation(summary = "获取收藏夹列表", description = "根据账号获取收藏夹列表")
    @Parameters({
            @Parameter(name = "account", description = "账号"),
            @Parameter(name = "postId", description = "要这个帖子获取收藏状态")
    })
    public Result listFavorites(@PathVariable("account") Long account, @PathVariable("postId") Integer postIdOptional) {
        return postService.listFavorites(OptionalLong.of(account), OptionalInt.of(postIdOptional));
    }
    @GetMapping("/list/mine")
    @Operation(summary = "获取收藏夹列表", description = "根据账号获取收藏夹列表")
    public Result listMyPosts() {
        return postService.listMyPosts();
    }

    @PostMapping("/favorite/create")
    @Operation(summary = "创建收藏夹", description = "创建一个新的收藏夹")
    @Parameters({
            @Parameter(name = "title", description = "收藏夹标题"),
            @Parameter(name = "account", description = "账号")
    })
    public Result createFavorite(@RequestBody CreateFavoriteDto dto) {
        return postService.createFavorite(Optional.of(dto));
    }

    @GetMapping("/hots")
    @Operation(summary = "获取热门帖子", description = "根据热度排序获取热门帖子")
    @Parameters({
            @Parameter(name = "account", description = "当前用户账号，用于判断是否点赞"),
            @Parameter(name = "limit", description = "返回的帖子数量，默认10条")
    })
    public Result getHotPosts(@RequestParam(value = "account", required = false) Optional<Long> account,
                              @RequestParam(value = "limit", required = false) Optional<Integer> limit) {
        return postService.getHotPosts(account, limit);
    }
}
